package com.yshebkb.sulp.job;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author yshebkb
 * @date 2018/9/17
 */
public class Main {
    public static void main(String[] args) {

        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("testPool-%d").build();

        ThreadPoolExecutor pool = new ThreadPoolExecutor(10, 20,
                0, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(1024), threadFactory, new ThreadPoolExecutor.AbortPolicy());

        List<String> companies = Arrays.asList("DH", "NH", "BY");
        List<ThreadTest> tasks = companies.stream().map(Main::create).collect(Collectors.toList());
        long start = System.currentTimeMillis();
        tasks.forEach(Thread::start);
        tasks.forEach(task -> {
            try {
                task.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        System.out.println("耗时: " + (System.currentTimeMillis() - start) / 1000 + "s");
        List<String> results = tasks.stream().map(ThreadTest::getResult).collect(Collectors.toList());
        results.forEach(System.out::println);
    }


    public static ThreadTest create(String name) {
        return new ThreadTest(name, "shanghai", "beijing");
    }
}
